package com.hilaver.dzmis.web.servlet.order;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.basicinfo.BiFranceCustomerEshop;
import com.hilaver.dzmis.order.OrderProduct;
import com.hilaver.dzmis.product.ProductIdentification;
import com.hilaver.dzmis.service.impl.OrderProductServiceImpl;
import com.hilaver.dzmis.util.StringUtils;
import com.hilaver.dzmis.vo.OrderStatisticsCriteria;

/**
 * Servlet implementation class for Servlet: OrderServlet
 * 
 */
public class OrderProductServlet extends
		com.hilaver.dzmis.web.servlet.AbstractBaseServlet implements
		javax.servlet.Servlet {
	static final long serialVersionUID = 1L;
	
	private OrderProduct orderProduct;
	private OrderProductServiceImpl orderProductService;
	public final static int ACTION_DO_STATISTICS = 20;

	public OrderProductServlet() {
		super();
		this.orderProductService = new OrderProductServiceImpl();
		this.orderProductService.setRealFileDir(UPLOAD_TEMP_REAL_DIR);
		this.fullName = OrderProduct.class.getName();
		this.simpleName = OrderProduct.class.getSimpleName();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
		orderProductService.setRealFileDir(UPLOAD_TEMP_REAL_DIR);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter writer = response.getWriter();
		try {
			String outputStr = execute(request);
			if(this.action == ACTION_EDIT){
				response.setContentType(Constants.HTML_RESPONSE_TYPE);
			}
			writer.write(outputStr);
		} catch (Exception e) {
			writer.print(e.getMessage());
			e.printStackTrace();
		}
		writer.close();
	}

	protected String execute(HttpServletRequest request) throws Exception {
		this.setAction(request);
		this.orderProductService.setLocale((Locale) request.getSession().getAttribute(
				Constants.SESSION_LOCALE));
		super.setAcUser(request, this.orderProductService);
		String rtn = "";
		List<String> filterList;
		switch (this.action) {
		case ACTOIN_GET_ALL_PAGINATION: // "getAllPagination";

			break;
		case ACTION_EDIT: // "edit";
			this.fillObj(request);
			this.orderProductService.edit(this.orderProduct);
			break;
		case ACTION_DELETE: // "delete";
			this.setId(request);
			rtn = this.orderProductService.delete(this.id);
			break;
		case ACTION_GET: // "get one"
			this.setId(request);
			rtn = this.orderProductService.get(this.id);
			break;
		case ACTION_GET_WITH_DESCRIPTION:
			this.setId(request);
			rtn = this.orderProductService.getWithDescription(this.id);
			break;
		case ACTION_GET_ALL_PAGINATION_FILTER: // "getAllPagination filter"
			this.setFilterParam(request);
			this.setPaginationParam(request);
			rtn = this.orderProductService.getAllPagination(page, sort, dir, filters);
			break;
		case ACTION_GET_ALL:
			this.setId(request);
			rtn = this.orderProductService.getAll(this.id);
			break;
		case ACTION_DO_STATISTICS:
			this.fillObj(OrderStatisticsCriteria.class.getName(), request);
			rtn = this.orderProductService.doStatistics((OrderStatisticsCriteria)this.obj);
			break;
		}
		return rtn;
	}

	protected void fillObj(HttpServletRequest request) throws Exception {
		super.fillObj(request);
		this.orderProduct = (OrderProduct)this.obj;
		String biPiIdStr = (String)getValue(request, "piId");
		if(biPiIdStr != null){
			Integer piId = Integer.parseInt(biPiIdStr);
			ProductIdentification pi = new ProductIdentification();
			pi.setId(piId);
			this.orderProduct.setProductIdentification(pi);
		}
		
		if("01".equals(this.orderProduct.getIsEshop())){
			BiFranceCustomerEshop bfce = new BiFranceCustomerEshop();
			bfce = (BiFranceCustomerEshop)super.fillObjRtn(BiFranceCustomerEshop.class.getName(), request);
			String biFranceCustomerEshopIdStr = (String)getValue(request, "biFranceCustomerEshopId");
			if(!StringUtils.isEmpty(biFranceCustomerEshopIdStr)){
				Integer biFranceCustomerEshopId = Integer.parseInt(biFranceCustomerEshopIdStr);
				bfce.setId(biFranceCustomerEshopId);
			}
			this.orderProduct.setBiFranceCustomerEshop(bfce);
		}
	}
	
}